package com.yboot.starter.shiro.core;

import com.yboot.starter.common.core.security.SecurityUserVO;
import com.yboot.starter.common.core.security.UserContext;
import com.yboot.starter.common.core.userdetails.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.util.Optional;

public class ShiroUserContext implements UserContext {

    @Override
    public Optional<SecurityUserVO> getUser() {
        Subject currentUser = SecurityUtils.getSubject();
        LoginUser loginUser = null;
        if (currentUser.getPrincipal() instanceof LoginUser) {
             loginUser = (LoginUser) currentUser.getPrincipal();
        }

        return Optional.ofNullable(new SecurityUserVO(loginUser.getId(), loginUser.getUsername(), loginUser.getTenantIds()));
    }
}